package com.cola.utils.templateMessageUtils.utils;

import com.alibaba.fastjson.JSON;
import com.cola.enums.TemplateMessageType;
import com.cola.utils.PropertyUtils;
import com.cola.utils.templateMessageUtils.entity.Data;
import com.cola.utils.templateMessageUtils.entity.newOrder.NewOrderMessage;
import com.cola.utils.templateMessageUtils.entity.newOrder.NewOrderMessageData;
import com.cola.utils.templateMessageUtils.entity.orderFinished.OrderFinishedMessage;
import com.cola.utils.templateMessageUtils.entity.orderFinished.OrderFinishedMessageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Max on 2017/8/18.
 * 新分配订单通知
 */
public class NewOrderMessageUtil {
    private static final Logger logger = LoggerFactory.getLogger(NewOrderMessageUtil.class);
//    {{first.DATA}}
//    订单编号：{{keyword1.DATA}}
//    预计总价：{{keyword2.DATA}}
//    订单类型：{{keyword3.DATA}}
//    接单时间：{{keyword4.DATA}}
//    {{remark.DATA}}

    /**
     * 发送新分配订单通知
     * @param orderNo
     * @param openId
     * @return
     */
    public static boolean send(String orderNo,String price,String openId,String orderTime){
        //获取消息实体json
        String messageJson = getMessageJson(orderNo,price,"微信渠道订单",openId,orderTime);

        //插入待发送模板消息列表
        boolean status = TemplateCommon.insetWaitingSendMessage(orderNo, TemplateMessageType.NEW_ORDER.name(),openId,messageJson);

        if(status){
            logger.error("新订单【"+orderNo+"】分配通知已成功插入待发送队列！");
        }else{
            logger.error("新订单【"+orderNo+"】分配通知插入待发送队列失败！");
        }
        return status;
    }

    /**
     * 获取模板消息发送请求Json
     * @param orderNo
     * @param openId
     * @param orderTime
     * @return
     */
    private static String getMessageJson(String orderNo,String price,String type,String openId,String orderTime){
        NewOrderMessageData messageData = new NewOrderMessageData();
        Data data = new Data();

        data.setValue(PropertyUtils.getProperty("new_order_message_title"));
        messageData.setFirst(data);

        data = new Data();
        data.setValue(orderNo);
        messageData.setKeyword1(data);

        data = new Data();
        data.setValue(price);
        messageData.setKeyword2(data);

        data = new Data();
        data.setValue(type);
        messageData.setKeyword3(data);

        data = new Data();
        data.setValue(orderTime);
        messageData.setKeyword4(data);

        data = new Data();
        data.setValue(PropertyUtils.getProperty("new_order_message_remark"));
        messageData.setRemark(data);

        NewOrderMessage message = new NewOrderMessage();

        message.setData(messageData);
        message.setTemplate_id(PropertyUtils.getProperty("new_order_template_id"));

        message.setTouser(openId);

        message.setUrl(PropertyUtils.getProperty("new_order_url"));

        return JSON.toJSONString(message);
    }

    public static void main(String[] args) {
        send("NO111","200.00","obOPQwiBMxtetnUh5kx4DTctz2is","2017-08-18");
    }


}
